// pages/summary_training/summary_training.js
var qiniuUploader = require("../../utils/qiniuUploader.js");
var util = require('../../utils/util');
var App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: '',
        crouse: '',
        name: '',
        corse_id: '',
        array: [
            {
                id: '1',
                name:'适度'
            },
            {
                id: '2',
                name: '中度'
            },
            {
                id: '3',
                name: '强烈'
            }],
        index:0,
        to_ken: '',//token值
        temp: [],//图片数组
        tr: '',//图片字符串
        list:[],
        training_results:'',//训练总结
        traning_sporttype: 1,//运动强度
        traning_sport:'',//运动方式
        traning_summer:'',//总结
        remarks:'',//备注
        index_p: 0,
        type:'',//1添加2编辑
        type_index:'1',
        start: '',
        end: '',
        membership_id: '', 
        startDate: '',
        endDate: '',
        see_details: '',//代表是私教课程的详情列表跳转的
        member_id:'',
        item_id:''
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var This = this;
        // console.log(options.list)
        This.setData({
            start: options.start,
            end: options.end,
            membership_id: options.membership_id,
            startDate: options.startDate,
            endDate: options.endDate,
            see_details: options.see_details,
            member_id: options.member_id,
            item_id: options.item_id
        })
        console.log(options.type)
        console.log(options.list)
        if (options.list==undefined||options.type == 2){
            console.log('1')
            if(options.type == 2){
                console.log('2')
                This.token();
                This.setData({
                    corse_id: options.b_id,
                    type: 2,//编辑
                    list: options.list
                })
                This.loading();
                setTimeout(function(){
                    var a = decodeURIComponent(options.list);
                    var b = JSON.parse(a)
                    var c = JSON.stringify(b)
                    This.setData({
                        list: c,
                    })
                    // console.log(This.data.list)
                },1000)
            }else {
                This.token();
                This.setData({
                    corse_id: options.b_id,
                    type: 2,//编辑
                })
                This.loading();
            }
            
        }else {
            console.log('3') 
            var a = decodeURIComponent(options.list);
            var b = JSON.parse(a)
            var c = JSON.stringify(b)
            console.log(b)
            console.log(c) 
            This.token();
            This.setData({
                time: options.time,
                crouse: options.crouse,
                name: options.name,
                corse_id: options.b_id,
                list: c,
                type: options.type,
            })
        } 
        console.log(This.data.list)
        // console.log(This.data.start)
    },
    // loading 编辑页面信息加载
    loading:function(){
        var This = this;
        App._get('coach/Coursearrange/coursePlanningList', {
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            phone: wx.getStorageSync('phone'),
            // openid: 1,
            // store_id: 53,
            // phone: 15612340001,
            b_id: This.data.corse_id
        }, function (res) {
            if (res.status == 1) {
                This.setData({
                    list: res.data,
                    type_index: res.data.exercise_intensity,
                    time: res.data.date_str + '  ' + res.data.start_time + '-' + res.data.end_time,
                    name: res.data.user_name,
                    crouse: res.data.course_name,
                    training_results: res.data.training_result,
                    traning_sport: res.data.exercise_mode,
                    traning_summer: res.data.summary,
                    remarks: res.data.desc,
                    list: JSON.stringify(res.data.action_data)
                })
                This.setData({
                    index_p: This.data.type_index - 1
                })
                var imgsplit = res.data.img.split(',');
                This.setData({
                    temp: imgsplit
                })
                console.log(This.data.list)
            } else {
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    // 返回编辑
    up_list:function(){
        var This = this;
        wx.navigateTo({
            url: '../curriculum_planning/curriculum_planning?b_id=' + This.data.corse_id + '&type=' + This.data.type + '&start=' + This.data.start + '&end=' + This.data.end + '&membership_id=' + This.data.membership_id + '&startDate=' + This.data.startDate + '&endDate=' + This.data.endDate + '&member_id=' + This.data.member_id + '&item_id=' + This.data.item_id + '&see_details=' + This.data.see_details + "&list=" + This.data.list + '&time=' + This.data.time + '&name=' + This.data.name + '&crouse=' + This.data.crouse,
        })
    },
    // 训练总结
    actionName:function(e){
        var This = this;
        This.setData({
            training_results: e.detail.value,
        });
        console.log(e)
    },
    // 训练方式
    timeClick: function (e) {
        var This = this;
        This.setData({
            traning_sport: e.detail.value,
        });
    },
    // 运动强度
    bindPickerChange:function(e){
        var This = this;
        // console.log(e)
        This.setData({
            index_p: e.detail.value,
            type_index: This.data.array[e.detail.value].id
        });
    },
    // 总结
    summers:function(e){
        var This= this;
        This.setData({
            traning_summer: e.detail.value,
        })
        // console.log(e)
    },
    // 备注
    remals:function(e){
        var This = this;
        console.log(e)
        This.setData({
            remarks: e.detail.value,
        })
    },
    // 完成的点击事件
    step:function(){
        var This = this;
        var imgBox = This.data.temp.join(',');
        console.log(This.data.list)
        console.log(imgBox)
        if(This.data.type != 2){
            App._post_form('coach/Coursearrange/coursePlanning', {
                phone: wx.getStorageSync('phone'),//手机号
                store_id: wx.getStorageSync('store_id'),//门店id  
                openid: wx.getStorageSync('openid'),//openid 
                b_id: This.data.corse_id,//课程id
                training_result: This.data.training_results,//训练总结-训练结果
                exercise_mode: This.data.traning_sport,//训练总结-运动方式
                exercise_intensity: This.data.type_index,//训练总结-运动强度 1适度 2中度 3强烈
                summary: This.data.traning_summer,//训练总结-总结
                desc: This.data.remarks,//训练总结-备注
                img: imgBox,//训练总结-照片
                list: This.data.list,
            }, function (result) {
                if (result.status == 1) {
                    // wx.showToast({
                    //     title: result.message,
                    //     icon: 'none',
                    //     duration: 2000
                    // })
                        wx.navigateTo({
                            url: '../curriculumScheduling/curriculumScheduling?start=' + This.data.start + '&end=' + This.data.end + '&membership_id=' + This.data.membership_id + '&res=' + 2 + '&startDate=' + This.data.startDate + '&endDate=' + This.data.endDate,
                        })
                    
                } else {
                    setTimeout(function(){
                        wx.showToast({
                            title: result.error,
                            icon: 'none',
                            duration: 2000
                        })
                    },0)
                    if (imgBox != '') {
                        imgBox = imgBox.split(',')
                        console.log(imgBox)
                        if (imgBox.length > 0) {
                            This.setData({
                                temp: imgBox
                            })
                        } else {
                            This.setData({
                                temp: []
                            })
                        }
                    }
                }
            }, false, function (res) {
                
            })
        }else if(This.data.type == 2){
            App._post_form('coach/Coursearrange/coursePlanningSave', {
                phone: wx.getStorageSync('phone'),//手机号
                store_id: wx.getStorageSync('store_id'),//门店id  
                openid: wx.getStorageSync('openid'),//openid 
                b_id: This.data.corse_id,//课程id
                training_result: This.data.training_results,//训练总结-训练结果
                exercise_mode: This.data.traning_sport,//训练总结-运动方式
                exercise_intensity: This.data.type_index,//训练总结-运动强度 1适度 2中度 3强烈
                summary: This.data.traning_summer,//训练总结-总结
                desc: This.data.remarks,//训练总结-备注
                img: imgBox,//训练总结-照片
                list: This.data.list,
            }, function (result) {
                if (result.status == 1) {
                    if (This.data.see_details == 1) {
                        wx.navigateTo({
                            url: '../details_private_education/details_private_education?item_id=' + This.data.item_id + '&member_id=' + This.data.member_id,
                        })
                    } else {
                        wx.navigateTo({
                            url: '../curriculumScheduling/curriculumScheduling?start=' + This.data.start + '&end=' + This.data.end + '&membership_id=' + This.data.membership_id + '&res=' + 2 + '&startDate=' + This.data.startDate + '&endDate=' + This.data.endDate,
                        })
                    }
                    wx.showToast({
                        title: result.message,
                        icon: 'none',
                        duration: 2000
                    })
                } else {
                    setTimeout(function () {
                        wx.showToast({
                            title: result.error,
                            icon: 'none',
                            duration: 2000
                        })
                    }, 0)
                    if (imgBox !=''){
                        imgBox = imgBox.split(',')
                        console.log(imgBox)
                        if (imgBox.length > 0) {
                            This.setData({
                                temp: imgBox
                            })
                        } else {
                            This.setData({
                                temp: []
                            })
                        }
                    } 
                }
            }, false, function (res) {
                console.log(res)
            })
        }  
    },
    didPressChooseImage: function () {
        var This = this
        wx.chooseImage({
            count: 1, // 默认9  
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有  
            success: function (res) {
                // const tempFilePaths = res.tempFilePaths
                console.log(res)

                const filepath = res.tempFilePaths[0]
                console.log(filepath)
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片    
                qiniuUploader.upload(filepath, res => {
                    console.log(res)

                    //七牛云js中封装的方法 成功的话就把链接push给data中的空数组
                    //https://image.chaotingtiyu.com/0.045449892188446084
                    This.data.temp.push('https://image.chaotingtiyu.com'+'/' + res.key)
                    This.setData({
                        //当push进去的链接大于一个时 就在底下显示缩略图 
                        temp: This.data.temp,
                    })
                    console.log(This.data.temp)
                }, (error) => {
                    This.setData({
                        error: error
                    })
                    console.log('error' + error)
                }, {
                        uploadURL: 'https://up-z1.qiniup.com/',
                        domain: 'https://image.chaotingtiyu.com/',
                        uptoken: This.data.to_ken,
                    })
            }
        })
    },
    del_img: function (e) {
        var This = this;
        console.log(e);
        var num = e.currentTarget.dataset.index;
        This.data.temp.splice(num, 1);
        This.setData({
            temp: This.data.temp
        })
        // console.log(This.data.temp)
    },
    token: function () {
        var This = this;
        App._get('coach/Qiniutoken/token', {
        },
            function (res) {
                // console.log(res)
                This.setData({
                    to_ken: res.data
                })
                // console.log(This.data.to_ken)
            }
        )
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})